/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FilePanel.java
 *
 * Created on Dec 25, 2009, 4:28:57 PM
 */
package cititool.chat.client.com;

import cititool.chat.client.ClientContext;
import cititool.chat.client.handler.WorkJob;
import cititool.chat.protocol.TransProtocol;
import java.awt.Cursor;
import java.awt.event.MouseAdapter;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

/**
 *
 * @author zx04741
 */
public class FilePanel extends javax.swing.JPanel {

    /** Creates new form FilePanel */
    public FilePanel(String recvName, WorkJob job) {
        initComponents();
        this.recvName = recvName;
        this.job = job;
        this.filenamelbl.setText(recvName);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        filenamelbl = new javax.swing.JLabel();
        progress = new javax.swing.JLabel();
        lbl3 = new javax.swing.JLabel();
        lbl1 = new javax.swing.JLabel();
        lbl2 = new javax.swing.JLabel();
        speed = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        jProgressBar1.setName("jProgressBar1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cititool.MainApp.class).getContext().getResourceMap(FilePanel.class);
        filenamelbl.setText(resourceMap.getString("filenamelbl.text")); // NOI18N
        filenamelbl.setName("filenamelbl"); // NOI18N

        progress.setText(resourceMap.getString("progress.text")); // NOI18N
        progress.setName("progress"); // NOI18N

        lbl3.setForeground(resourceMap.getColor("lbl3.foreground")); // NOI18N
        lbl3.setText(resourceMap.getString("lbl3.text")); // NOI18N
        lbl3.setName("lbl3"); // NOI18N
        lbl3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lbl3MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lbl3MouseExited(evt);
            }
        });

        lbl1.setForeground(resourceMap.getColor("lbl1.foreground")); // NOI18N
        lbl1.setText(resourceMap.getString("lbl1.text")); // NOI18N
        lbl1.setName("lbl1"); // NOI18N
        lbl1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lbl1MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lbl1MouseExited(evt);
            }
        });

        lbl2.setForeground(resourceMap.getColor("lbl2.foreground")); // NOI18N
        lbl2.setText(resourceMap.getString("lbl2.text")); // NOI18N
        lbl2.setName("lbl2"); // NOI18N
        lbl2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                lbl2MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                lbl2MouseExited(evt);
            }
        });

        speed.setText(resourceMap.getString("speed.text")); // NOI18N
        speed.setName("speed"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(speed, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lbl1)
                        .addGap(18, 18, 18)
                        .addComponent(lbl2)
                        .addGap(18, 18, 18)
                        .addComponent(lbl3))
                    .addComponent(filenamelbl, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(filenamelbl, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(progress, javax.swing.GroupLayout.DEFAULT_SIZE, 16, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lbl3, javax.swing.GroupLayout.DEFAULT_SIZE, 16, Short.MAX_VALUE)
                        .addComponent(lbl2)
                        .addComponent(lbl1))
                    .addComponent(speed, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void lbl1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbl1MouseEntered
        // TODO add your handling code here:
        this.setCursor(new Cursor(Cursor.HAND_CURSOR) );
    }//GEN-LAST:event_lbl1MouseEntered

    private void lbl1MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbl1MouseExited
        // TODO add your handling code here:
        this.setCursor(Cursor.getDefaultCursor());
    }//GEN-LAST:event_lbl1MouseExited

    private void lbl2MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbl2MouseEntered
        // TODO add your handling code here:
        this.setCursor(new Cursor(Cursor.HAND_CURSOR) );
    }//GEN-LAST:event_lbl2MouseEntered

    private void lbl3MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbl3MouseEntered
        // TODO add your handling code here:
        this.setCursor(new Cursor(Cursor.HAND_CURSOR) );
    }//GEN-LAST:event_lbl3MouseEntered

    private void lbl2MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbl2MouseExited
        // TODO add your handling code here:
        this.setCursor(Cursor.getDefaultCursor());
    }//GEN-LAST:event_lbl2MouseExited

    private void lbl3MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbl3MouseExited
        // TODO add your handling code here:
        this.setCursor(Cursor.getDefaultCursor());
    }//GEN-LAST:event_lbl3MouseExited

    public void setSender() {
        lbl1.setVisible(false);
        lbl2.setVisible(false);
        lbl3.setText("cancel");
        lbl3.addMouseListener(new MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if (evt.getClickCount() == 1) {
                    job.cancel();
                }
            }
        });
        lbl3.setVisible(true);
    }

    public JProgressBar getProcessBar() {

        return jProgressBar1;
    }

    public JLabel getProgressLabel() {

        return progress;
    }

    public JLabel getSpeedLabel() {

        return speed;
    }

    public void setRecv() {
        lbl1.setVisible(true);
        lbl2.setVisible(true);
        //accept the file to default folder
        lbl1.addMouseListener(new MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if (evt.getClickCount() == 1) {
                    saveFilepath = ClientContext.getCurrentUserFolder() + File.separator + recvName;
                    try {
                        TransProtocol.writeStr(TransProtocol.READY_TRANSFER_FH + saveFilepath, ClientContext.getCurrentSocket());
                    } catch (IOException ex) {
                        ClientContext.warnLog("start receive file " + saveFilepath + " error:", ex);
                    }
                    lbl2.setVisible(false);
                    lbl1.setVisible(false);
                }
            }
        });
        lbl2.addMouseListener(new MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if (evt.getClickCount() == 1) {
                    JFileChooser chooser = new JFileChooser();
                    chooser.setMultiSelectionEnabled(false);
                    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                    saveFilepath = chooser.getSelectedFile().getPath();
                    try {
                        TransProtocol.writeStr(TransProtocol.READY_TRANSFER_FH + saveFilepath, ClientContext.getCurrentSocket());
                    } catch (IOException ex) {
                        ClientContext.warnLog("start receive file " + saveFilepath + " error:", ex);
                    }
                    lbl2.setVisible(false);
                    lbl1.setVisible(false);
                }
            }
        });

        lbl3.addMouseListener(new MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                if (evt.getClickCount() == 1) {
                    job.cancel();
                }
            }
        });
        lbl3.setVisible(true);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel filenamelbl;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JLabel lbl1;
    private javax.swing.JLabel lbl2;
    private javax.swing.JLabel lbl3;
    private javax.swing.JLabel progress;
    private javax.swing.JLabel speed;
    // End of variables declaration//GEN-END:variables
    private String recvName;
    private WorkJob job;
    private String saveFilepath;
}
